const { sequelize } = require('../utils/sequelize');
const CourseService = require('./CourseService');

class StudentCourseService {
    constructor() {
    }

    async saveData(obj){
        let data = null;
        let dataObj = {
            courseId: obj.courseId,
            studentId: obj.studentId,
            status: 0
        }
        data = await sequelize.models.StudentCourse.create({...dataObj})

        return data;
    }

    async findCourseList(pageSize, pageNum, status, studentId) {
        let data = null;
        let offset = pageSize * (pageNum - 1);
            let arrObj = await sequelize.models.StudentCourse.findAndCountAll({
                offset: offset,
                limit: pageSize * 1,
                where: {
                    status,
                    studentId
                }
            });
            let courseRows = arrObj.rows;
            for(let i=0;i<courseRows.length;i++){
                let roomAddr = await CourseService.getAttr(courseRows[i].courseId, ['courseName']);
                courseRows[i].setDataValue('courseName', roomAddr.getDataValue('courseName'));
            }
            data = arrObj;
        return data;
    }

}

module.exports = new StudentCourseService();